<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>可拖拽登录框</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        .login-header {
            width: 100%;
            text-align: center;
            height: 30px;
            font-size: 24px;
            line-height: 30px;
        }
        
        a {
            text-decoration: none;
            color: #000000;
        }
        
        .login {
            display: none;
            width: 512px;
            height: 280px;
            position: fixed;
            left: 50%;
            top: 50%;
            background: #ffffff;
            box-shadow: 0px 0px 20px #ddd;
            border: #ebebeb solid 1px;
            /* 半透明光芒 */
            z-index: 9999;
            /* 使元素处于最上方 */
            transform: translate(-50%, -50%);
            /* 移动元素的-50% */
        }
        
        .login-title {
            position: relative;
            width: 100%;
            text-align: center;
            line-height: 40px;
            height: 40px;
            font-size: 18px;
            margin: 10px 0px 0px 0px;
            cursor: move;
            /* 自定义光标为移动光标 */
        }
        
        .close-login {
            position: absolute;
            width: 40px;
            height: 40px;
            right: -20px;
            top: -20px;
            font-size: 12px;
            background-color: #ffffff;
            line-height: 40px;
            text-align: center;
            border-radius: 20px;
            border: #ebebeb solid 1px;
            box-shadow: 0px 0px 5px #ddd;
        }
        
        .login-input-content {
            margin-top: 20px;
            text-align: center;
            line-height: 35px;
        }
        
        .login-input {
            overflow: hidden;
            font-size: 14px;
            margin: 0px 0px 20px 0px;
        }
        
        .login-input label {
            float: left;
            width: 90px;
            height: 35px;
            text-align: right;
            padding-right: 10px;
            line-height: 35px;
            font-size: 14px;
        }
        
        .login-input input.list-input {
            float: left;
            line-height: 35px;
            width: 350px;
            height: 35px;
            border: #ebebeb 1px solid;
            text-indent: 5px;
        }
        
        .login-button {
            width: 50%;
            margin: 30px auto 0px auto;
            line-height: 40px;
            font-size: 14px;
            border: #ebebeb 1px solid;
        }
        
        .bg {
            display: none;
            background-color: rgba(0, 0, 0, .3);
            position: fixed;
            top: 0px;
            left: 0px;
            width: 100%;
            height: 100%;
        }
    </style>
</head>

<body>
    <div class="login-header">
        <a id="link" href="javascript:;">点击弹出登录框</a>
    </div>
    <div class="login">
        <div class="login-title" id="login-title">登陆会员<span><a  href="javascript:;;" class="close-login">关闭</a></span></div>

        <div class="login-input-content">
            <div class="login-input">
                <label>用户名：</label>
                <input type="text" placeholder="请输入用户名" name="username" class="list-input">

            </div>
            <div class="login-input">
                <label>登陆密码：</label>
                <input type="text" placeholder="请输入密码" name="password" class="list-input">
            </div>
            <div class="login-button">
                <a href="javascript:;" id="login-button-submit">登陆会员</a>
            </div>
        </div>
    </div>
    <!-- 灰色遮罩层 -->
    <div class="bg"></div>
    <script>
        var link = document.querySelector('#link');
        var bg = document.querySelector('.bg');
        var login = document.querySelector('.login');
        link.addEventListener('click', function() {
            bg.style.display = 'block';
            login.style.display = 'block';
        })
        var closer = document.querySelector('.close-login')
        closer.addEventListener('click', function() {
            bg.style.display = 'none';
            login.style.display = 'none';
        })

        // 拖拽效果
        var title = document.querySelector('#login-title');
        title.addEventListener('mousedown', function(e) {
            var x = e.pageX - login.offsetLeft;
            var y = e.pageY - login.offsetTop;
            console.log(x);
            console.log(y);
            document.addEventListener('mousemove', move)

            function move(e) {
                login.style.left = e.pageX - x + 'px';
                login.style.top = e.pageY - y + 'px';
                // login.style.transform = 0;
            }
            title.addEventListener('mouseup', function() {
                document.removeEventListener('mousemove', move)
            })


        })
    </script>
</body>

</html>